package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Key;
import com.dotstone.chipism.util.KeyManager;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class KeyPairAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<Key> mKeyList;
	private Context context;
	private int imageResId;
	private int stringResId;
	private OnClickListener mDeleteKeyListener;
	private OnClickListener onTestKeyListener;
	private int bgDrawableResId;

	// private OnTestKeyListener onTestKeyListener;
	public KeyPairAdapter(Context context, OnClickListener mDeleteKeyListener, OnClickListener onTestKeyListener) {
		this.context = context;
		this.mDeleteKeyListener = mDeleteKeyListener;
		this.onTestKeyListener = onTestKeyListener;
		inflater = LayoutInflater.from(context);
	}

	public void setData(List<Key> mKeyList) {
		this.mKeyList = mKeyList;
	}
	
	public List<Key> getData(){
		return mKeyList;
	}

	@Override
	public int getCount() {

		return mKeyList.size();
	}

	@Override
	public Object getItem(int position) {

		return mKeyList.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		convertView = inflater.inflate(R.layout.item_pair_key, null);
		if(position%2==0){
			convertView.setBackgroundColor(Color.WHITE);
		}else{
			convertView.setBackgroundColor(context.getResources().getColor(R.color.background_gray));
		}
		Key key = mKeyList.get(position);
		LinearLayout signInfoLayout = (LinearLayout) convertView.findViewById(R.id.sign_info_layout);
		final EditText nameEd = (EditText) convertView.findViewById(R.id.key_name_ed);
		ImageView keyImg = (ImageView) convertView.findViewById(R.id.key_img);
		TextView keyTv = (TextView) convertView.findViewById(R.id.key_tv);
		ImageView downpullImg = (ImageView) convertView.findViewById(R.id.downpull_img);
		TextView signLenghTv = (TextView) convertView.findViewById(R.id.sign_lengh_tv);
		ImageView deleteImg = (ImageView) convertView.findViewById(R.id.delete_img);
		deleteImg.setTag(position);
		Button mTestBtn = (Button) convertView.findViewById(R.id.test_btn);
		mTestBtn.setTag(position);
		FrameLayout keyIconLayout = (FrameLayout) convertView.findViewById(R.id.key_icon_layout);
		bgDrawableResId = KeyManager.getInstance().getBackGroundResId(key.getKeyNum());
		keyIconLayout.setBackgroundResource(bgDrawableResId);
		nameEd.setText(key.getName());
		if (key.getCodeValue() != null) {
			signLenghTv.setText(" " + key.getCodeValue().length());
		}
		if (key.getContentType() == 0) {
			imageResId = KeyManager.getInstance().getDrawableResId(key.getKeyNum());
			keyTv.setVisibility(View.GONE);
			keyImg.setImageResource(imageResId);
		} else {
			stringResId = KeyManager.getInstance().getStringResId(key.getKeyNum());
			keyImg.setVisibility(View.GONE);
			keyTv.setText(stringResId);
		}
		switch (key.getIsSignLayoutShowm()) {
		case 0:
			downpullImg.setImageResource(R.drawable.biao1);
			signInfoLayout.setVisibility(View.GONE);
			break;
		case 1:
			downpullImg.setImageResource(R.drawable.biao2);
			signInfoLayout.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}
		nameEd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				nameEd.requestFocus();
			}
		});
		nameEd.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				nameEd.setTextColor(context.getResources()
						.getColor(R.color.gray_introdution));
				mKeyList.get(position).setName(arg0.toString());
			}
		});
		deleteImg.setOnClickListener(mDeleteKeyListener);
		mTestBtn.setOnClickListener(onTestKeyListener);
		return convertView;
	}

	public interface OnDeleteKeyListener {
		public void deleteKey(int position);
	}

	public interface OnTestKeyListener {
		public void deleteKey(int position);
	}

	class HoldView {
		TextView keyTv;
		EditText nameEd;
		ImageView keyImg, selectedImg;
		LinearLayout keyInfoLayout, signInfoLayout;
		Button mTestBtn;

		public HoldView(View view) {
			keyImg = (ImageView) view.findViewById(R.id.key_img);
			nameEd = (EditText) view.findViewById(R.id.key_name_ed);
			selectedImg = (ImageView) view.findViewById(R.id.selected_img);
			keyTv = (TextView) view.findViewById(R.id.key_tv);
		}
	}
}